/*******************************************************************************
 * Copyright 2011 Alexandre Zglav and Morphiastic
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package com.morphiastic.analysis.filters;

import java.io.IOException;
import java.util.Map;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;

import com.morphiastic.analysis.AnalysisComponentDefinition;
import com.morphiastic.utils.EqualsUtil;

public class NGramTokenFilter extends ElasticFilter{

	
	public static final String MIN_GRAM_PARAM_KEY = "min_gram";
	public static final String MAX_GRAM_PARAM_KEY = "max_gram";
	
	 
	
	private Integer minGram;
	private Integer maxGram;
	
	
	public NGramTokenFilter(String defName) {
		this(null, defName);
	}

	public NGramTokenFilter(Map<String,String> params, String defName) {
		super(ElasticFilterType.nGram, defName);
		
		if(params!=null){
			String mingStr = params.get(MIN_GRAM_PARAM_KEY);
			if(mingStr!=null && !mingStr.isEmpty()){
				try {
					int ming = Integer.parseInt(mingStr);
					this.minGram = ming;
				} catch (NumberFormatException e) {
				}
			}
			
			String maxgStr = params.get(MAX_GRAM_PARAM_KEY);
			if(maxgStr!=null && !maxgStr.isEmpty()){
				try {
					int maxg = Integer.parseInt(maxgStr);
					this.maxGram = maxg;
				} catch (NumberFormatException e) {
				}
			}
		}
	}
	
	

	@Override
	public boolean hasCustomParams() {
		 if(this.minGram!= null || this.maxGram!=null)return true;
		 return false;
	}
	
	@Override
	public XContentBuilder addToBuilder(
			XContentBuilder mappingBuilder) throws IOException {

		mappingBuilder.startObject(getDefinitionName())
		.field("type", getComponentSubTypeName());
		
		if(this.minGram != null){
			mappingBuilder.field(MIN_GRAM_PARAM_KEY, this.minGram);
		}
		
		if(this.maxGram != null){
			mappingBuilder.field(MAX_GRAM_PARAM_KEY, this.maxGram);
		}
		return mappingBuilder;
	}
	
	
	@Override
	public void setParamsFromSettings(Settings settings) {
		
		String minGSettings = settings.get(MIN_GRAM_PARAM_KEY);
		if(minGSettings!=null && !minGSettings.isEmpty()){
			try {
				int mtl = Integer.parseInt(minGSettings);
				this.minGram = mtl;
			} catch (NumberFormatException e) {
			}
		}
		
		String maxGSettings = settings.get(MAX_GRAM_PARAM_KEY);
		if(maxGSettings!=null && !maxGSettings.isEmpty()){
			try {
				int mtl = Integer.parseInt(maxGSettings);
				this.maxGram = mtl;
			} catch (NumberFormatException e) {
			}
		}
	}
	
	@Override
	public boolean areEquivalentDefinitions(AnalysisComponentDefinition otherTok){
		if(otherTok==null)return false;
		NGramTokenFilter ot ;
		try {
			ot = (NGramTokenFilter)otherTok;
		} catch (ClassCastException e) {
			return false;
		}
		return
		EqualsUtil.areEqual(this.minGram, ot.minGram)
		&&
		EqualsUtil.areEqual(this.maxGram, ot.maxGram);
	}

	
	
	
	
	
	
	
	
	
	
	/**
	 * @return the minGram
	 */
	public Integer getMinGram() {
		return minGram;
	}

	/**
	 * @param minGram the minGram to set
	 */
	public void setMinGram(Integer minGram) {
		this.minGram = minGram;
	}

	/**
	 * @return the maxGram
	 */
	public Integer getMaxGram() {
		return maxGram;
	}

	/**
	 * @param maxGram the maxGram to set
	 */
	public void setMaxGram(Integer maxGram) {
		this.maxGram = maxGram;
	}
		
	
	
	
}
